<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  <title>mongodb的安装配置与开启审计日志 | 清峰小栈</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  
  <meta name="keywords" content="mongodb" />
  
  
  
  
  <meta name="description" content="MongoDB的安装配置与开启审计日志 欢迎来到知道的越多，不知道的越多系列！  1、Mongo的安装教程（Linux）1、下载安装包 https:&#x2F;&#x2F;www.mongodb.com&#x2F;download-center&#x2F;community2、上传安装包3、解压，移动 12tar -zxvf mongodb-linux-x86_64-4.0.6.tgzmv .&#x2F;mongodb-linux-x86_64-">
<meta property="og:type" content="article">
<meta property="og:title" content="MongoDB的安装配置与开启审计日志">
<meta property="og:url" content="http://example.com/2021/09/11/%E8%BF%90%E7%BB%B4%E5%AE%89%E8%A3%85/MongoDB%E7%9A%84%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%AE%E4%B8%8E%E5%BC%80%E5%90%AF%E5%AE%A1%E8%AE%A1%E6%97%A5%E5%BF%97/index.html">
<meta property="og:site_name" content="清峰小栈">
<meta property="og:description" content="MongoDB的安装配置与开启审计日志 欢迎来到知道的越多，不知道的越多系列！  1、Mongo的安装教程（Linux）1、下载安装包 https:&#x2F;&#x2F;www.mongodb.com&#x2F;download-center&#x2F;community2、上传安装包3、解压，移动 12tar -zxvf mongodb-linux-x86_64-4.0.6.tgzmv .&#x2F;mongodb-linux-x86_64-">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2021-09-11T09:12:42.000Z">
<meta property="article:modified_time" content="2023-05-04T06:56:33.801Z">
<meta property="article:author" content="清峰">
<meta property="article:tag" content="mongodb">
<meta name="twitter:card" content="summary">
  

  

  <link rel="icon" href="/qingfeng-blog/css/images/mylogo-2.png">
  <link rel="apple-touch-icon" href="/qingfeng-blog/css/images/mylogo-2.png">
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  <link href="https://fonts.googleapis.com/css?family=Open+Sans|Montserrat:700" rel="stylesheet" type="text/css">
  <link href="https://fonts.googleapis.com/css?family=Roboto:400,300,300italic,400italic" rel="stylesheet" type="text/css">
  <!-- <link href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css" rel="stylesheet"> -->
  <link href="/qingfeng-blog/css/font-awesome.css" rel="stylesheet">
  <style type="text/css">
    @font-face{font-family:futura-pt; src:url("/qingfeng-blog/css/fonts/FuturaPTBold.otf") format("woff");font-weight:500;font-style:normal;}
    @font-face{font-family:futura-pt-light; src:url("/qingfeng-blog/css/fonts/FuturaPTBook.otf") format("woff");font-weight:lighter;font-style:normal;}
    @font-face{font-family:futura-pt-italic; src:url("/qingfeng-blog/css/fonts/FuturaPTBookOblique.otf") format("woff");font-weight:400;font-style:italic;}
}

  </style>
  
<link rel="stylesheet" href="/qingfeng-blog/css/style.css">


  
<script src="/qingfeng-blog/js/jquery-3.1.1.min.js"></script>

  
<script src="/qingfeng-blog/js/bootstrap.js"></script>


  <!-- Bootstrap core CSS -->
  <link rel="stylesheet" href="/qingfeng-blog/css/bootstrap.css" >

  
    
<link rel="stylesheet" href="/qingfeng-blog/css/dialog.css">

  

  

  
    <link rel="stylesheet" href="/qingfeng-blog/css/header-post.css" >
  

  
  
  
    <link rel="stylesheet" href="/qingfeng-blog/css/vdonate.css" >
  

<meta name="generator" content="Hexo 6.3.0"></head>



  <body data-spy="scroll" data-target="#toc" data-offset="50">


  
  <div id="container">
    <div id="wrap">
      
        <header>

    <div id="allheader" class="navbar navbar-default navbar-static-top" role="navigation">
        <div class="navbar-inner">
          
          <div class="container"> 
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
              <span class="sr-only">Toggle navigation</span>
              <span class="icon-bar"></span>
              <span class="icon-bar"></span>
              <span class="icon-bar"></span>
            </button>

            
              <a class="brand" style="
                 border-width: 0px;  margin-top: 0px;"  
                href="#" data-toggle="modal" data-target="#myModal" >
                  <img width="155px" height="90px" alt="Hike News" src="/qingfeng-blog/css/images/mylogo-2.png">
              </a>
            
            
            <div class="navbar-collapse collapse">
              <ul class="hnav navbar-nav">
                
                  <li> <a class="main-nav-link" href="/qingfeng-blog/">首页</a> </li>
                
                  <li> <a class="main-nav-link" href="/qingfeng-blog/categories">分类</a> </li>
                
                  <li> <a class="main-nav-link" href="/qingfeng-blog/tags">标签</a> </li>
                
                  <li><div id="search-form-wrap">

    <form class="search-form">
        <input type="text" class="ins-search-input search-form-input" placeholder="" />
        <button type="submit" class="search-form-submit"></button>
    </form>
    <div class="ins-search">
    <div class="ins-search-mask"></div>
    <div class="ins-search-container">
        <div class="ins-input-wrapper">
            <input type="text" class="ins-search-input" placeholder="Type something..." />
            <span class="ins-close ins-selectable"><i class="fa fa-times-circle"></i></span>
        </div>
        <div class="ins-section-wrapper">
            <div class="ins-section-container"></div>
        </div>
    </div>
</div>
<script>
(function (window) {
    var INSIGHT_CONFIG = {
        TRANSLATION: {
            POSTS: 'Posts',
            PAGES: 'Pages',
            CATEGORIES: 'Categories',
            TAGS: 'Tags',
            UNTITLED: '(Untitled)',
        },
        ROOT_URL: '/qingfeng-blog/',
        CONTENT_URL: '/qingfeng-blog/content.json',
    };
    window.INSIGHT_CONFIG = INSIGHT_CONFIG;
})(window);
</script>

<script src="/qingfeng-blog/js/insight.js"></script>


</div></li>
            </div>
          </div>
                
      </div>
    </div>

</header>



      
            
      <div id="content" class="outer">
        
          <section id="main" style="float:none;"><article id="post-运维安装/MongoDB的安装配置与开启审计日志" style="width: 75%; float:left;" class="article article-type-post" itemscope itemprop="blogPost" >
<div id="container">
  <div id="articleInner" class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="thumb" class="article-title" itemprop="name">
      MongoDB的安装配置与开启审计日志
    </h1>
  

      </header>
    
    <div class="article-meta">
      
	<a href="/qingfeng-blog/2021/09/11/%E8%BF%90%E7%BB%B4%E5%AE%89%E8%A3%85/MongoDB%E7%9A%84%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%AE%E4%B8%8E%E5%BC%80%E5%90%AF%E5%AE%A1%E8%AE%A1%E6%97%A5%E5%BF%97/" class="article-date">
	  <time datetime="2021-09-11T09:12:42.000Z" itemprop="datePublished">2021-09-11</time>
	</a>

      
    <a class="article-category-link" href="/qingfeng-blog/categories/Linux/">Linux</a>

      
	<a class="article-views">
	<span id="busuanzi_container_page_pv">
		PV:<span id="busuanzi_value_page_pv"></span>
	</span>
	</a>

      

    </div>
    <div class="article-entry" itemprop="articleBody">
      
        <h1 id="MongoDB的安装配置与开启审计日志"><a href="#MongoDB的安装配置与开启审计日志" class="headerlink" title="MongoDB的安装配置与开启审计日志"></a>MongoDB的安装配置与开启审计日志</h1><blockquote>
<p>欢迎来到知道的越多，不知道的越多系列！</p>
</blockquote>
<h2 id="1、Mongo的安装教程（Linux）"><a href="#1、Mongo的安装教程（Linux）" class="headerlink" title="1、Mongo的安装教程（Linux）"></a>1、Mongo的安装教程（Linux）</h2><p><strong>1、下载安装包</strong></p>
<p><a target="_blank" rel="noopener" href="https://www.mongodb.com/download-center/community">https://www.mongodb.com/download-center/community</a><br><strong>2、上传安装包</strong><br><strong>3、解压，移动</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">tar -zxvf mongodb-linux-x86_64-4.0.6.tgz</span><br><span class="line">mv ./mongodb-linux-x86_64-4.0.6 /usr/local/mongodb</span><br></pre></td></tr></table></figure>
<p><strong>4、进去mongodb文件夹</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">cd /usr/local/mongodb/</span><br></pre></td></tr></table></figure>

<p><strong>5、创建db的目录和日志文件夹</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">mkdir -p ./data/db</span><br><span class="line">mkdir -p ./logs</span><br><span class="line">touch ./logs/mongodb.log</span><br></pre></td></tr></table></figure>

<p><strong>6、创建mongo配置文件</strong></p>
<p>​	6.1、conf配置文件形式：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">vim mongodb.conf</span><br><span class="line">#端口号</span><br><span class="line">port=27017</span><br><span class="line">#数据目录</span><br><span class="line">dbpath=/usr/local/mongodb/data/db</span><br><span class="line">#日志目录</span><br><span class="line">logpath=/usr/local/mongodb/logs/mongodb.log</span><br><span class="line">#后台启动</span><br><span class="line">fork=true</span><br><span class="line">#追加日志输出</span><br><span class="line">logappend=true</span><br><span class="line">#允许远程IP连接（所有）</span><br><span class="line">bind_ip=0.0.0.0</span><br></pre></td></tr></table></figure>

<p>​	6.2、yml配置文件形式：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">processManagement:</span><br><span class="line">   fork: true</span><br><span class="line">net:</span><br><span class="line">   bindIp: 127.0.0.1</span><br><span class="line">   port: 27017</span><br><span class="line">storage:</span><br><span class="line">   dbPath: 数据存放路径/data/db</span><br><span class="line">systemLog:</span><br><span class="line">   destination: file</span><br><span class="line">   path: log/mongo27017.log</span><br><span class="line">   logAppend: true</span><br><span class="line">storage:</span><br><span class="line">   journal:</span><br><span class="line">      enabled: true</span><br></pre></td></tr></table></figure>

<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">processManagement:</span><br><span class="line">   fork: true</span><br><span class="line">net:</span><br><span class="line">   bindIp: 0.0.0.0</span><br><span class="line">   port: 27017</span><br><span class="line">storage:</span><br><span class="line">   dbPath: /usr/local/mongodb/data/db</span><br><span class="line">systemLog:</span><br><span class="line">   destination: file</span><br><span class="line">   path: /usr/local/mongodb/logs/mongodb.log</span><br><span class="line">   logAppend: true</span><br><span class="line">storage:</span><br><span class="line">   journal:</span><br><span class="line">      enabled: true</span><br><span class="line"># 改动</span><br><span class="line">security:</span><br><span class="line">  # 开启认证</span><br><span class="line">  authorization: enabled</span><br><span class="line">  # 认证文件的路径</span><br><span class="line">  keyFile: &quot;/usr/local/mongodb/mongodb-keyfile&quot;</span><br><span class="line">  clusterAuthMode: keyFile</span><br><span class="line"></span><br><span class="line"># 改动</span><br><span class="line">replication:</span><br><span class="line">  oplogSizeMB: 10240</span><br><span class="line">  # 副本集名称</span><br><span class="line">  replSetName: replSet</span><br></pre></td></tr></table></figure>

<p><strong>7、配置文件启动</strong></p>
<p>​	7.1、conf配置文件启动：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">./bin/mongod --config mongodb.conf</span><br></pre></td></tr></table></figure>

<p>​	7.2、yml配置文件形式启动：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">./bin/mongod --config mongodb.yml</span><br></pre></td></tr></table></figure>

<p><strong>8、连接到mongo</strong></p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">./bin/mongo</span><br></pre></td></tr></table></figure>

<h2 id="2、安装中遇到的问题："><a href="#2、安装中遇到的问题：" class="headerlink" title="2、安装中遇到的问题："></a><strong>2、安装中遇到的问题：</strong></h2><p>1、配置文件启动时报错</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">error while loading shared libraries: libcrypto.so.10: cannot open shared object file: No such file or directory</span><br><span class="line">原因：没有装libcrypto.so.10库</span><br><span class="line">解决方案：安装yum -y install compat-openssl10</span><br></pre></td></tr></table></figure>

<p>2、配置文件启动时报错</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">error while loading shared libraries: libnetsnmpmibs.so.35:  cannot open shared object file: No such file or directory</span><br><span class="line">原因：没有装net-snmp</span><br><span class="line">解决方案：安装yum install net-snmp</span><br></pre></td></tr></table></figure>

<h2 id="3、创建mongo账户"><a href="#3、创建mongo账户" class="headerlink" title="3、创建mongo账户"></a>3、创建mongo账户</h2><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">1、Read：允许用户读取指定数据库</span><br><span class="line">2、readWrite：允许用户读写指定数据库</span><br><span class="line">3、dbAdmin：允许用户在指定数据库中执行管理函数，如索引创建、删除，查看统计或访问system.profile</span><br><span class="line">4、userAdmin：允许用户向system.users集合写入，可以找指定数据库里创建、删除和管理用户</span><br><span class="line">5、clusterAdmin：只在admin数据库中可用，赋予用户所有分片和复制集相关函数的管理权限。</span><br><span class="line">6、readAnyDatabase：只在admin数据库中可用，赋予用户所有数据库的读权限</span><br><span class="line">7、readWriteAnyDatabase：只在admin数据库中可用，赋予用户所有数据库的读写权限</span><br><span class="line">8、userAdminAnyDatabase：只在admin数据库中可用，赋予用户所有数据库的userAdmin权限</span><br><span class="line">9、dbAdminAnyDatabase：只在admin数据库中可用，赋予用户所有数据库的dbAdmin权限。</span><br><span class="line">10、root：只在admin数据库中可用。超级账号，超级权限</span><br></pre></td></tr></table></figure>

<p>创建管理员账户</p>
<ul>
<li><p>切换到admin数据库 </p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">use admin</span><br></pre></td></tr></table></figure>
</li>
<li><p>添加账户(root：最高权限任意操作，userAdminAnyDatabase:管理用户权限)</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db.createUser(&#123; user: &quot;root&quot;, pwd: &quot;Zz@123456&quot;, roles: [&#123; role: &quot;root&quot;, db: &quot;admin&quot; &#125;] &#125;)</span><br></pre></td></tr></table></figure>


</li>
<li><p>修改mongo.conf文件, 增加配置</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">auth=true</span><br><span class="line">或者yml格式的加入这个配置</span><br><span class="line">security:</span><br><span class="line">  authorization: enabled</span><br></pre></td></tr></table></figure>
</li>
<li><p>重启MongoDB服务</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">#停掉服务</span><br><span class="line">ps -ef|grep mongo</span><br><span class="line">kill -9 PID</span><br><span class="line">#根据配置文件启动服务</span><br><span class="line">./bin/mongod --config mongodb.conf</span><br></pre></td></tr></table></figure>
</li>
<li><p>连接mongo</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">./mongo</span><br></pre></td></tr></table></figure>
</li>
<li><p>认证：（未认证查看或操作）</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">use admin</span><br><span class="line">db.auth(&quot;root&quot;,&quot;Zz@123456&quot;)  #返回1，认证成功</span><br></pre></td></tr></table></figure>



<h3 id="2）创建普通用户"><a href="#2）创建普通用户" class="headerlink" title="2）创建普通用户"></a>2）创建普通用户</h3></li>
<li><p>管理员账户认证</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">use admin</span><br><span class="line">db.auth(&quot;root&quot;,&quot;Zz@123456&quot;)</span><br></pre></td></tr></table></figure>
</li>
<li><p>切换到指定库</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">use 指定库名</span><br></pre></td></tr></table></figure>
</li>
<li><p>创建普通账户，给定权限</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db.createUser(&#123; user: &quot;user&quot;, pwd: &quot;123456&quot;, roles: [&#123; role: &quot;readWrite&quot;, db: &quot;test数据库名称&quot; &#125;] &#125;)</span><br></pre></td></tr></table></figure></li>
</ul>
<p>注意：创建成功，无需重启</p>
<ul>
<li><p>更新角色权限(root最高权限)</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db.updateUser(&quot;root&quot;,&#123;roles : [&#123;&quot;role&quot; : &quot;root&quot;,&quot;db&quot; : &quot;admin&quot;&#125;]&#125;)</span><br></pre></td></tr></table></figure></li>
</ul>
<h2 id="4、开启mongo审计功能"><a href="#4、开启mongo审计功能" class="headerlink" title="4、开启mongo审计功能"></a>4、开启mongo审计功能</h2><p><a target="_blank" rel="noopener" href="https://blog.csdn.net/zhaoyuqiang/article/details/105361948">https://blog.csdn.net/zhaoyuqiang/article/details/105361948</a></p>
<p><strong>1、概述</strong></p>
<p>在数据库安全的生命周期中，包括：保护、检测、响应及补救。检测的核心就是审计（Audit）。</p>
<p>审计功能可以用来记录用户对数据库的所有相关操作。这些记录可以让系统管理员在需要的时候分析数据库在什么时段发生了什么事情。</p>
<p>mongodb分为社区版和企业版，只有企业版MongoDB Enterprise才有审计功能。</p>
<p>mongodb的企业版下载链接：</p>
<p><a target="_blank" rel="noopener" href="https://www.mongodb.com/try/download/enterprise">https://www.mongodb.com/try/download/enterprise</a><br>安装mongodb的rpm包时会提示缺少依赖包，可通过yum的方式安装所需的依赖包</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yum install net-snmp cyrus-sasl cyrus-sasl-plain cyrus-sasl-gssapi</span><br></pre></td></tr></table></figure>



<blockquote>
<p>注意：配置文件采用yml格式</p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">auditLog:</span><br><span class="line">  destination: 审计展示类型（console窗口展示、syslog系统日志保存、file文件保存。）</span><br><span class="line">  format: 在JSON文件或者BSON文件中</span><br><span class="line">  path: 保存路径</span><br><span class="line">  filter: 日志过滤条件</span><br><span class="line">setParameter: &#123; auditAuthorizationSuccess: true &#125;</span><br><span class="line"></span><br><span class="line"># 审计日志设置（此过滤能满足绝大部分的日志记录，减少记录无效日志）</span><br><span class="line">auditLog:</span><br><span class="line">  destination: file</span><br><span class="line">  format: JSON</span><br><span class="line">  path: /usr/local/mongodb/data/auditLog/audit.json</span><br><span class="line">  filter: &#x27;&#123; atype:&#123; $in: [&quot;authCheck&quot;,&quot;createCollection&quot;,&quot;createDatabase&quot;] &#125;,&quot;param.ns&quot;:&#123;$nin:[  &quot;admin.system.version&quot;]&#125;,&quot;param.command&quot;:&#123;$nin:[  &quot;isMaster&quot;,&quot;ismaster&quot;,&quot;saslStart&quot;,&quot;saslContinue&quot;,&quot;listCollections&quot;,&quot;listDatabases&quot;,&quot;listIndexes&quot;,&quot;collStats&quot;,&quot;find&quot;,&quot;getlasterror&quot;,&quot;buildinfo&quot;,&quot;getLastError&quot;,&quot;aggregate&quot;]  &#125;  &#125;&#x27;</span><br><span class="line">setParameter: &#123; auditAuthorizationSuccess: true &#125;</span><br></pre></td></tr></table></figure>

<p>注意：日志则保存在audit.json中，可通过tail -f audit.json实时在线查看日志打印情况。</p>
<h2 id="5、日志详情介绍"><a href="#5、日志详情介绍" class="headerlink" title="5、日志详情介绍"></a>5、日志详情介绍</h2><p>任何一种数据库都有各种各样的日志，MongoDB也不例外。MongoDB中有4种日志，分别是系统日志、Journal日志、oplog主从日志、慢查询日志等。这些日志记录着MongoDB数据库不同方面的踪迹。下面分别介绍这几种日志。</p>
<p><strong>1、系统日志</strong></p>
<p>系统日志在MongoDB数据库中很重要，它记录着MongoDB启动和停止的操作，以及服务器在运行过程中发生的任何异常信息。<br>配置系统日志的方法比较简单，在启动mongod时指定logpath参数即可</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">mongod -logpath=/data/log/mongodb/serverlog.log -logappend</span><br></pre></td></tr></table></figure>

<p>系统日志会向logpath指定的文件持续追加。</p>
<p><strong>2、Journal日志</strong>（一般配置文件中开启）</p>
<p>journaling(日记) 日志功能则是 MongoDB 里面非常重要的一个功能 ， 它保证了数据库服务器在意外断电 、 自然灾害等情况下数据的完整性。它通过预写式的redo日志为MongoDB增加了额外的可靠性保障。开启该功能时,MongoDB会在进行写入时建立一条Journal日志,其中包含了此次写入操作具体更改的磁盘地址和字节。因此一旦服务器突然停机，可在启动时对日志进行重放，从而重新执行那些停机前没能够刷新到磁盘的写入操作</p>
<p><strong>3、oplog主从日志</strong></p>
<p>Replica Sets复制集用于在多台服务器之间备份数据。MongoDB的复制功能是使用操作日志oplog实现的，操作日志包含了主节点的每一次写操作。</p>
<p>一个mongod实例中的所有数据库都使用同一个oplog，也就是所有数据库的操作日志(插入，删除，修改)都会记录到oplog中</p>
<p><strong>4、慢查询日志</strong></p>
<p>MongoDB中使用系统分析器(system profiler)来查找耗时过长的操作。系统分析器记录固定集合system.profile中的操作，并提供大量有关耗时过长的操作信息，但相应的mongod的整体性能也会有所下降。因此我们一般定期打开分析器来获取信息。</p>
<p>默认情况下，系统分析器处于关闭状态，不会进行任何记录。可以在shell中运行db.setProfilingLevel()开启分析器</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db.setProfilingLevel(``level``,&lt;slowms&gt;) 0=``off` `1=slow 2=``all</span><br></pre></td></tr></table></figure>

<p>第一个参数是指定级别，不同的级别代表不同的意义，0表示关闭，1表示默认记录耗时大于100毫秒的操作，2表示记录所有操作。第二个参数则是自定义“耗时过长”标准，比如记录所有耗时操作500ms的操作</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">db.setProfilingLevel(1,500);</span><br></pre></td></tr></table></figure>

<p>如果开启了分析器而system.profile集合并不存在，MongoDB会为其建立一个大小为若干MB的固定集合(capped collection)。如希望分析器运行更长时间，可能需要更大的空间记录更多的操作。此时可以关闭分析器，删除并重新建立一个新的名为system.profile的固定集合，并令其容量符合要求。然后在数据库上重新启用分析器。</p>
<blockquote>
<p>可以通过db.system.profile.stats()查看集合的最大容量.</p>
</blockquote>
<p><strong>总结</strong></p>
<p>以上就是这篇文章的全部内容了，希望本文的内容对大家的学习或者工作具有一定的参考学习价值。</p>

      
    </div>
    <footer class="article-footer">
      
        <div id="donation_div"></div>


<script src="/qingfeng-blog/js/vdonate.js"></script>

<script>
var a = new Donate({
  title: '如果觉得我的文章对您有用，请随意打赏。您的支持将鼓励我继续创作!', // 可选参数，打赏标题
  btnText: 'Donate', // 可选参数，打赏按钮文字
  el: document.getElementById('donation_div'),
  wechatImage: 'http://img.lindaifeng.vip/typora-picgo-tuchaung/20230428150832.png',
  alipayImage: 'http://img.lindaifeng.vip/typora-picgo-tuchaung/20230428150823.png'
});
</script>
      
      
      <div>
        <ul class="post-copyright">
          <li class="post-copyright-author">
          <strong>Post author:  </strong>清峰</a>
          </li>
          <li class="post-copyright-link">
          <strong>Post link:  </strong>
          <a href="/qingfeng-blog/2021/09/11/运维安装/MongoDB的安装配置与开启审计日志/" target="_blank" title="MongoDB的安装配置与开启审计日志">http://example.com/2021/09/11/运维安装/MongoDB的安装配置与开启审计日志/</a>
          </li>
          <li class="post-copyright-license">
            <strong>Copyright Notice:   </strong>
            All articles in this blog are licensed under <a rel="license" href="https://creativecommons.org/licenses/by-nc-nd/4.0/" target="_blank" title="Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)">CC BY-NC-ND 4.0</a>
            unless stating additionally.
          </li>
         
        </ul>
<div>

      
      
        
	<div id="comment">
		<!-- 来必力City版安装代码 -->
		<div id="lv-container" data-id="city" data-uid="MTAyMC8yOTQ4MS82MDQ5">
		<script type="text/javascript">
		   (function(d, s) {
		       var j, e = d.getElementsByTagName(s)[0];

		       if (typeof LivereTower === 'function') { return; }

		       j = d.createElement(s);
		       j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
		       j.async = true;

		       e.parentNode.insertBefore(j, e);
		   })(document, 'script');
		</script>
		<noscript>为正常使用来必力评论功能请激活JavaScript</noscript>
		</div>
		<!-- City版安装代码已完成 -->
	</div>



      
      
        
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/qingfeng-blog/tags/mongodb/" rel="tag">mongodb</a></li></ul>

      

    </footer>
  </div>
</div>
  
    
<nav id="article-nav">
  
    <a href="/qingfeng-blog/2021/09/14/%E8%BF%90%E7%BB%B4%E5%AE%89%E8%A3%85/Linux%E5%AE%89%E8%A3%85mongo%E4%B8%BB%E4%BB%8E%E6%A8%A1%E5%BC%8F/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          Linux安装mongo主从模式
        
      </div>
    </a>
  
  
    <a href="/qingfeng-blog/2021/09/11/%E8%BF%90%E7%BB%B4%E5%AE%89%E8%A3%85/Linux%E5%AE%89%E8%A3%85mysql/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">Linux安装mysql</div>
    </a>
  
</nav>

  
</article>

<!-- Table of Contents -->

  <aside id="toc-sidebar">
    <div id="toc" class="toc-article">
    <strong class="toc-title">Contents</strong>
    
        <ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#MongoDB%E7%9A%84%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%AE%E4%B8%8E%E5%BC%80%E5%90%AF%E5%AE%A1%E8%AE%A1%E6%97%A5%E5%BF%97"><span class="nav-number">1.</span> <span class="nav-text">MongoDB的安装配置与开启审计日志</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1%E3%80%81Mongo%E7%9A%84%E5%AE%89%E8%A3%85%E6%95%99%E7%A8%8B%EF%BC%88Linux%EF%BC%89"><span class="nav-number">1.1.</span> <span class="nav-text">1、Mongo的安装教程（Linux）</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2%E3%80%81%E5%AE%89%E8%A3%85%E4%B8%AD%E9%81%87%E5%88%B0%E7%9A%84%E9%97%AE%E9%A2%98%EF%BC%9A"><span class="nav-number">1.2.</span> <span class="nav-text">2、安装中遇到的问题：</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3%E3%80%81%E5%88%9B%E5%BB%BAmongo%E8%B4%A6%E6%88%B7"><span class="nav-number">1.3.</span> <span class="nav-text">3、创建mongo账户</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2%EF%BC%89%E5%88%9B%E5%BB%BA%E6%99%AE%E9%80%9A%E7%94%A8%E6%88%B7"><span class="nav-number">1.3.1.</span> <span class="nav-text">2）创建普通用户</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4%E3%80%81%E5%BC%80%E5%90%AFmongo%E5%AE%A1%E8%AE%A1%E5%8A%9F%E8%83%BD"><span class="nav-number">1.4.</span> <span class="nav-text">4、开启mongo审计功能</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5%E3%80%81%E6%97%A5%E5%BF%97%E8%AF%A6%E6%83%85%E4%BB%8B%E7%BB%8D"><span class="nav-number">1.5.</span> <span class="nav-text">5、日志详情介绍</span></a></li></ol></li></ol>
    
    </div>
  </aside>


</section>
        
      </div>
      
      <footer id="footer">
  

  <div class="container">
      	<div class="row">
	      <!-- <p> Powered by <a href="http://hexo.io/" target="_blank">Hexo</a> and <a href="https://github.com/iTimeTraveler/hexo-theme-hiker" target="_blank">Hexo-theme-hiker</a> </p> -->
	      <p>May there be no bug in the world！</p>
        <p id="copyRightEn">Copyright &copy; 2022 - 2023 All Rights Reserved.</p>
	      
	      
    		<p class="busuanzi_uv">
				UV : <span id="busuanzi_value_site_uv"></span> |  
				PV : <span id="busuanzi_value_site_pv"></span>
		    </p>
  		   
		</div>

		
  </div>
</footer>


<!-- min height -->

<script>
    var wrapdiv = document.getElementById("wrap");
    var contentdiv = document.getElementById("content");
    var allheader = document.getElementById("allheader");

    wrapdiv.style.minHeight = document.body.offsetHeight + "px";
    if (allheader != null) {
      contentdiv.style.minHeight = document.body.offsetHeight - allheader.offsetHeight - document.getElementById("footer").offsetHeight + "px";
    } else {
      contentdiv.style.minHeight = document.body.offsetHeight - document.getElementById("footer").offsetHeight + "px";
    }
</script>
    </div>
    <!-- <nav id="mobile-nav">
  
    <a href="/qingfeng-blog/" class="mobile-nav-link">首页</a>
  
    <a href="/qingfeng-blog/categories" class="mobile-nav-link">分类</a>
  
    <a href="/qingfeng-blog/tags" class="mobile-nav-link">标签</a>
  
</nav> -->
    

<!-- mathjax config similar to math.stackexchange -->

<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    tex2jax: {
      inlineMath: [ ['$','$'], ["\\(","\\)"] ],
      processEscapes: true
    }
  });
</script>

<script type="text/x-mathjax-config">
    MathJax.Hub.Config({
      tex2jax: {
        skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
      }
    });
</script>

<script type="text/x-mathjax-config">
    MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for(i=0; i < all.length; i += 1) {
            all[i].SourceElement().parentNode.className += ' has-jax';
        }
    });
</script>

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>


  
<link rel="stylesheet" href="/qingfeng-blog/fancybox/jquery.fancybox.css">

  
<script src="/qingfeng-blog/fancybox/jquery.fancybox.pack.js"></script>




<script src="/qingfeng-blog/js/scripts.js"></script>



  
<script src="/qingfeng-blog/js/home.js"></script>




  
<script src="/qingfeng-blog/js/dialog.js"></script>









	<div style="display: none;">
    <script src="https://s95.cnzz.com/z_stat.php?id=1260716016&web_id=1260716016" language="JavaScript"></script>
  </div>



	<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js">
	</script>






  </div>

  <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="display: none;">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h2 class="modal-title" id="myModalLabel">设置</h2>
      </div>
      <hr style="margin-top:0px; margin-bottom:0px; width:80%; border-top: 3px solid #000;">
      <hr style="margin-top:2px; margin-bottom:0px; width:80%; border-top: 1px solid #000;">


      <div class="modal-body">
          <div style="margin:6px;">
            <a data-toggle="collapse" data-parent="#accordion" href="#collapseOne" onclick="javascript:setFontSize();" aria-expanded="true" aria-controls="collapseOne">
              正文字号大小
            </a>
          </div>
          <div id="collapseOne" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingOne">
          <div class="panel-body">
            您已调整页面字体大小
          </div>
        </div>
      


          <div style="margin:6px;">
            <a data-toggle="collapse" data-parent="#accordion" href="#collapseTwo" onclick="javascript:setBackground();" aria-expanded="true" aria-controls="collapseTwo">
              夜间护眼模式
            </a>
        </div>
          <div id="collapseTwo" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingTwo">
          <div class="panel-body">
            夜间模式已经开启，再次单击按钮即可关闭 
          </div>
        </div>

        <div>
            <a data-toggle="collapse" data-parent="#accordion" href="#collapseThree" aria-expanded="true" aria-controls="collapseThree">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;关 于&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</a>
        </div>
         <div id="collapseThree" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingThree">
          <div class="panel-body">
            清峰小栈
          </div>
          <div class="panel-body">
            Copyright © 2023 清峰 All Rights Reserved.
          </div>
        </div>
      </div>


      <hr style="margin-top:0px; margin-bottom:0px; width:80%; border-top: 1px solid #000;">
      <hr style="margin-top:2px; margin-bottom:0px; width:80%; border-top: 3px solid #000;">
      <div class="modal-footer">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
      </div>
    </div>
  </div>
</div>
  
  <a id="rocket" href="#top" class=""></a>
  <script type="text/javascript" src="/qingfeng-blog/js/totop.js?v=1.0.0" async=""></script>
  
    <a id="menu-switch"><i class="fa fa-bars fa-lg"></i></a>
  
</body>
</html>